/**
 * 
 */
package com.pxingg.model.pubsub;

import junit.framework.Assert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.pxingg.Global;
import com.pxingg.log.LogConfig;

/**
 * @author pxingg
 * 
 */
public class SwitchCenterTest
{
    /**
     * The log of this class
     */
    static Log log = LogFactory.getLog(SwitchCenterTest.class);

    /**
     * @throws java.lang.Exception
     */
    @BeforeClass
    public static void setUpBeforeClass() throws Exception
    {
        // 打开日志开关
        LogConfig.init();
    }

    /**
     * @throws java.lang.Exception
     */
    @AfterClass
    public static void tearDownAfterClass() throws Exception
    {
    }

    /**
     * 测试时，订阅者和消息的名字
     */
    String name = "com.pxingg.SwitchCenterTest";

    /**
     * 使用全局的消息中心进行测试
     */
    SwitchCenter switchCenter = Global.switchCenter;

    /**
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception
    {
    }

    /**
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception
    {
    }

    /**
     * Test method for {@link com.pxingg.model.pubsub.SwitchCenter#addSubscriber(java.lang.String, com.pxingg.model.pubsub.Subscriber)}.
     */
    @Test
    public final void testAddSubscriber()
    {
        switchCenter.removeAllSubscriber();

        final Subscriber subscriber = new Subscriber(name);
        switchCenter.addSubscriber(name, subscriber);
        Assert.assertTrue(Global.switchCenter.toString().contains(" = " + name));

        // 删除掉已经订阅的消息
        Assert.assertTrue(switchCenter.removeSubscriber(name, subscriber));
        Assert.assertFalse(Global.switchCenter.toString().contains(" = " + name));
    }

    /**
     * Test method for {@link com.pxingg.model.pubsub.SwitchCenter#getSubscribers(java.lang.String)}.
     */
    @Test
    public final void testGetSubscribers()
    {
        switchCenter.removeAllSubscriber();

        final Subscriber subscriber = new Subscriber(name);
        switchCenter.addSubscriber(name, subscriber);
        Assert.assertTrue(switchCenter.toString().contains(" = " + name));

        Assert.assertEquals(1, switchCenter.getSubscribers(name).size());

        // 多次订阅，只保留一份
        switchCenter.addSubscriber(name, subscriber);
        switchCenter.addSubscriber(name, subscriber);
        switchCenter.addSubscriber(name, subscriber);

        Assert.assertEquals(1, switchCenter.getSubscribers(name).size());

        // 多次订阅，原来已经存在返回false
        Assert.assertFalse(switchCenter.addSubscriber(name, subscriber));

    }

    /**
     * Test method for {@link com.pxingg.model.pubsub.SwitchCenter#publish(com.pxingg.model.pubsub.Message)}.
     */
    @Test
    public final void testPublish()
    {
        final Message message = new Message(name);
        switchCenter.publish(message);
    }

    /**
     * Test method for {@link com.pxingg.model.pubsub.SwitchCenter#removeAllSubscriber()}.
     */
    @Test
    public final void testRemoveAllSubscriber()
    {
        switchCenter.removeAllSubscriber();

        final Subscriber subscriber = new Subscriber(name);
        switchCenter.addSubscriber(name, subscriber);
        Assert.assertTrue(Global.switchCenter.toString().contains(" = " + name));

        switchCenter.addSubscriber(name + "test", subscriber);
        Assert.assertTrue(Global.switchCenter.toString().contains(name + "test"));

        // 删除掉已经订阅的消息,两个都会被删除
        switchCenter.removeAllSubscriber();
        Assert.assertFalse(Global.switchCenter.toString().contains(" = " + name));
        Assert.assertFalse(Global.switchCenter.toString().contains(name + "test"));
    }

    /**
     * Test method for {@link com.pxingg.model.pubsub.SwitchCenter#removeSubscriber(java.lang.String, com.pxingg.model.pubsub.Subscriber)}.
     */
    @Test
    public final void testRemoveSubscriberStringSubscriber()
    {
        switchCenter.removeAllSubscriber();

        final Subscriber subscriber = new Subscriber(name);
        switchCenter.addSubscriber(name, subscriber);
        Assert.assertTrue(Global.switchCenter.toString().contains(" = " + name));

        // 删除掉已经订阅的消息
        Assert.assertTrue(switchCenter.removeSubscriber(name, subscriber));
        Assert.assertFalse(Global.switchCenter.toString().contains(" = " + name));

    }

    /**
     * Test method for {@link com.pxingg.model.pubsub.SwitchCenter#removeSubscriber(com.pxingg.model.pubsub.Subscriber)}.
     */
    @Test
    public final void testRemoveSubscriberSubscriber()
    {
        switchCenter.removeAllSubscriber();

        final Subscriber subscriber = new Subscriber(name);
        switchCenter.addSubscriber(name, subscriber);
        Assert.assertTrue(Global.switchCenter.toString().contains(" = " + name));

        switchCenter.addSubscriber(name + "test", subscriber);
        Assert.assertTrue(Global.switchCenter.toString().contains(name + "test"));

        // 删除掉已经订阅的消息,两个都会被删除
        Assert.assertTrue(switchCenter.removeSubscriber(subscriber));
        Assert.assertFalse(Global.switchCenter.toString().contains(" = " + name));
        Assert.assertFalse(Global.switchCenter.toString().contains(name + "test"));
    }

    /**
     * Test method for {@link com.pxingg.model.pubsub.SwitchCenter#shutdown()}.
     */
    @Test
    public final void testShutdown()
    {
        final SwitchCenter sw = new SwitchCenter();
        sw.shutdown();
    }

    /**
     * Test method for {@link com.pxingg.model.pubsub.SwitchCenter#toString()}.
     */
    @Test
    public final void testToString()
    {
        switchCenter.removeAllSubscriber();

        final Subscriber subscriber = new Subscriber(name);
        switchCenter.addSubscriber(name, subscriber);
        Assert.assertTrue(Global.switchCenter.toString().contains(" = " + name));

    }
}
